﻿/*
* Author: Zero
* Version: 1.2
* Clase que se encarga de crear la comunicación con el server y de recibir sus eventos
* Esos eventos u errores son reportados al BRIDGE
* +La central es la que ahora "desarma" el message y le saca el id
* +CENTRAL.as construye las acciones y sólo le dispara a Communication un sendAction()
*/

import com.dinmax.flash.central.*
import com.dinmax.flash.central.action.*
import com.dinmax.flash.central.bridge.*
import com.dinmax.flash.central.communication.*
import com.dinmax.flash.central.extension.*
import com.dinmax.flash.central.extension.state.*
import com.dinmax.flash.central.extension.message.*
import com.dinmax.flash.central.extension.user.*


class CENTRAL
implements CommunicationEventListener {
	
	private static var instance:CENTRAL;
	private static var activeExtensionId:Number;
		
	private var bridge:CENTRALBridge;
		
	private static function setActiveExtensionId(extensionID:Number):Void {
		//trace("active extension ID= "+extensionID);
		CENTRAL.activeExtensionId = extensionID;
		return;
	};
	
	public static function getActiveExtensionId():Number {
		return CENTRAL.activeExtensionId;
	};
	
	public static function newInstance(bridge:CENTRALBridge):Void {
		instance= new CENTRAL(bridge);
		return;
	}; //fin funci	
	
	public static function getActive():CENTRAL {
		return instance;
	}; //fin funci	
	
	function CENTRAL(bridge:CENTRALBridge){
		this.bridge = bridge;
		Communication.newInstance(this);
		return;
	};
	
	public function logIn(user:String, password:String):Void{
		if(Communication.getActive().doCommunication()){
			Communication.getActive().sendAction(new ActionLogin(user, password));
		};
		return;
	};
	
	public function logOut():Void {
		Communication.getActive().close();
		return;
	};
	
	public function outgoingCall(to:Number):Void {
		Communication.getActive().sendAction(new ActionOutgoingCall(CENTRAL.getActiveExtensionId(), to));	
		return;
	};
	
	public function refreshVoiceMessages(extId:Number):Void {
		Communication.getActive().sendAction(new ActionRefreshVoiceMessages(extId));
		return;
	};
	public function readMessage(msg, extId:Number):Void {
		Communication.getActive().sendAction(new ActionReadMessage(msg.getId(), extId));
		return;
	};
	
	public function deleteMessage(msg:Message, extId:Number):Void {
		if(msg instanceof TextMessage){
			Communication.getActive().sendAction(new ActionDeleteMessage(msg.getId(), extId));
		} else if (msg instanceof VoiceMessage) {
			Communication.getActive().sendAction(new ActionDeleteVoiceMessage(msg.getId(), extId, msg.getState()));
		} else {
			throw new Error("CENTRAL.as | deleteMessage() no reconozco la instancia");
		};
		return;
	};
		
	public function sendMessage(extId:Number, subject:String, body:String):Void {
		Communication.getActive().sendAction(new ActionSendMessage(CENTRAL.getActiveExtensionId(), extId, subject, body));
		return;
	};
	
	public function saveContact(userId:Number, contact:Contact):Void {
		Communication.getActive().sendAction(new ActionSaveContact(userId, contact.getId(), contact.getName(), contact.getLastName(), contact.getNick(), contact.getPhone(), contact.getEmail(), contact.getVisibility()));
		return;
	};
	
	public function deleteContact(contactId:Number):Void {
		Communication.getActive().sendAction(new ActionDeleteContact(contactId));
		return;
	};
	
	public function addShortcut(name:String, email:String, phone:Number):Void {
		Communication.getActive().sendAction(new ActionAddShortcut(CENTRAL.getActiveExtensionId(), name, email, phone));
		return;
	};
	
		
// METODOS DE INTERFACE CommunicationEventListener
	
	 function event(event:CommunicationEvent):Void {
		if(CommunicationEventSetExtensions(event)){
			bridge.CENTRAL_2_BRIDGE_setExtensions(CommunicationEventSetExtensions(event).getData(), CENTRAL.getActiveExtensionId());
		} else if (CommunicationEventLoginResponse(event)){
			CENTRAL.setActiveExtensionId(CommunicationEventLoginResponse(event).getData());
		} else if (CommunicationEventRefreshShortcuts(event)){
			var a:Array = CommunicationEventRefreshShortcuts(event).getData();
			bridge.getConsole().setShortCuts(a);
		} else if (CommunicationEventGetPersonsResponse(event)){
			var a:Array = CommunicationEventGetPersonsResponse(event).getData();
			bridge.getConsole().getRootButtons().getDATARoot().getPersonsResponse(a);
		};
	 };

	 function eventException(exception:Error):Void{
		trace("Central.as | eventException() "+exception);
		return;
	 };

//

}; // fin de clase